home *** CD-ROM | disk | FTP | other *** search
- Path: io.UWinnipeg.ca!wsimpson
- From: Bill Simpson <wsimpson@uwinnipeg.ca>
- Newsgroups: comp.lang.c
- Subject: start array at k, not 0
- Date: Thu, 4 Jan 1996 10:02:10 -0600
- Organization: University of Winnipeg
- Message-ID: <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
- NNTP-Posting-Host: io.uwinnipeg.ca
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=US-ASCII
-
- I have come up with the following 2 methods that allow one to talk about
- an array that starts at element k rather than 0. E.g. 10 element array
- y[k] to y[k+10]. Both seem to work. Is this illusory? Is one of the
- 2 ways better (or a way I haven't mentioned)?
-
- These programs set up y[5] to y[14].
-
- Thanks very much for any comments.
-
- Bill Simpson
-
- /*method 1*/
- #include <stdio.h>
-
- int main(void)
- {
- int i, x[10];
- int* y;
- int offset=5; /*ie 1st index at 5, y[5]-y[14] */
- y=x-offset;
-
- printf("offset=%d\n",offset);
-
- for (i=offset;i<10+offset;i++)
- {
- y[i]=i;
- printf("%d\n",y[i]);
- }
- return 0;
- }
-
- /*method 2*/
- #include <stdio.h>
- #include <stdlib.h>
-
- int main(void)
- {
- int i;
- int* y;
- int offset=5;
- int n=10;
- y=malloc(n*sizeof(int));
-
- printf("offset=%d\n",offset);
-
- for (i=offset;i<n+offset;i++)
- {
- y[i]=i;
- printf("%d\n",y[i]);
- }
- return 0;
- }
-
-
-